package com.markerhub.controller;

import com.markerhub.common.lang.Result;
import com.markerhub.entity.InspectionPlan;
import com.markerhub.service.InspectionPlanService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

/**
 * 巡检计划Controller
 */
@Api(tags = "巡检计划管理")
@RestController
@RequestMapping("/api/inspection-plan")
public class InspectionPlanController extends BaseController {

    @Autowired
    private InspectionPlanService inspectionPlanService;

    /**
     * 获取巡检计划列表
     */
    @ApiOperation(value = "获取巡检计划列表", notes = "获取所有巡检计划信息")
    @GetMapping("/list")
    public Result list() {
        List<InspectionPlan> list = inspectionPlanService.list();
        return Result.succ(200, "获取巡检计划列表成功", list);
    }

    /**
     * 查询巡检计划（支持分页和条件查询）
     */
    @ApiOperation(value = "查询巡检计划", notes = "支持分页和条件查询")
    @GetMapping("/query")
    public Result query(@RequestParam(required = false) String planName, 
                       @RequestParam(required = false) Integer status, 
                       @RequestParam(required = true) Integer pageNum, 
                       @RequestParam(required = true) Integer pageSize) {
        // 创建分页对象
        Page<InspectionPlan> page = new Page<>(pageNum, pageSize);
        // 构建查询条件
        QueryWrapper<InspectionPlan> wrapper = new QueryWrapper<>();
        if (planName != null && !planName.isEmpty()) {
            wrapper.like("plan_name", planName);
        }
        if (status != null) {
            wrapper.eq("status", status);
        }
        // 按创建时间倒序排序
        wrapper.orderByDesc("created");
        // 执行分页查询
        Page<InspectionPlan> pageData = inspectionPlanService.page(page, wrapper);
        return Result.succ(200, "查询巡检计划成功", pageData);
    }

    /**
     * 根据ID获取巡检计划详情
     */
    @ApiOperation(value = "获取巡检计划详情", notes = "根据ID获取巡检计划详细信息")
    @GetMapping("/detail/{id}")
    public Result detail(@PathVariable Long id) {
        InspectionPlan plan = inspectionPlanService.getById(id);
        return Result.succ(200, "获取巡检计划详情成功", plan);
    }

    /**
     * 新增巡检计划
     */
    @ApiOperation(value = "新增巡检计划", notes = "创建新的巡检计划")
    @PostMapping("/add")
    public Result add(@RequestBody InspectionPlan plan) {
        boolean saved = inspectionPlanService.save(plan);
        return saved ? Result.succ(200, "新增巡检计划成功", null) : Result.fail("新增巡检计划失败");
    }

    /**
     * 更新巡检计划
     */
    @ApiOperation(value = "更新巡检计划", notes = "更新现有巡检计划信息")
    @PutMapping("/update")
    public Result update(@RequestBody InspectionPlan plan) {
        boolean updated = inspectionPlanService.updateById(plan);
        return updated ? Result.succ(200, "更新巡检计划成功", null) : Result.fail("更新巡检计划失败");
    }

    /**
     * 删除巡检计划
     */
    @ApiOperation(value = "删除巡检计划", notes = "根据ID删除巡检计划")
    @DeleteMapping("/delete/{id}")
    public Result delete(@PathVariable Long id) {
        boolean deleted = inspectionPlanService.removeById(id);
        return deleted ? Result.succ(200, "删除巡检计划成功", null) : Result.fail("删除巡检计划失败");
    }

    /**
     * 启用/禁用巡检计划
     */
    @ApiOperation(value = "启用/禁用巡检计划", notes = "更新巡检计划的状态")
    @PutMapping("/enable")
    public Result enable(@RequestBody Map<String, Object> params) {
        Long id = Long.valueOf(params.get("id").toString());
        Integer status = Integer.valueOf(params.get("status").toString());
        
        InspectionPlan plan = new InspectionPlan();
        plan.setId(id);
        plan.setStatus(status);
        
        boolean updated = inspectionPlanService.updateById(plan);
        return updated ? Result.succ(200, "更新状态成功", null) : Result.fail("更新状态失败");
    }
}